package com.app.GoCuro;

import java.util.ArrayList;

import com.app.GoCuro.util.CustomHttpClient;
import com.app.GoCuro.util.DatabaseInformation;
import com.app.GoCuro.util.LoginResponse;

import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class SelectDatabaseActivity extends BaseActivity {

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.select_database);

		final ListView databaseList = (ListView) findViewById(R.id.database_list);
		final LoginResponse loginResponse = (LoginResponse) getIntent()
				.getSerializableExtra("curr_user");

		Resources res = getResources();
		CustomHttpClient client = new CustomHttpClient(this);
		final ArrayList<DatabaseInformation> userDatabases = client
				.getUserDatabases();

		// do not setup the database items if no databases were returned
		if (userDatabases == null)
			return;

		// convert the names of the databases into ArrayList<String> for passing
		// to the ArrayAdapter
		ArrayList<String> databaseNames = new ArrayList<String>();
		for (int i = 0; i < userDatabases.size(); i++) {
			databaseNames.add(userDatabases.get(i).getName());
		}

		ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
				android.R.layout.simple_list_item_1, databaseNames);
		databaseList.setAdapter(adapter);

		// add the on click listener
		databaseList.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int pos,
					long arg3) {
				/*
				 * Intent availableActionsIntent = new
				 * Intent(SelectDatabaseActivity.this,
				 * AvailableActionsActivity.class);
				 * 
				 * //pass in the database name and id for item retrieval
				 * availableActionsIntent.putExtra("selected_database",
				 * userDatabases.get(pos));
				 * availableActionsIntent.putExtra("curr_user", loginResponse);
				 * SelectDatabaseActivity
				 * .this.startActivity(availableActionsIntent);
				 */

				Intent TabLayoutActivityIntent = new Intent(
						SelectDatabaseActivity.this, TabLayoutActivity.class);

				// pass in the database name and id for item retrieval
				TabLayoutActivityIntent.putExtra("selected_database",
						userDatabases.get(pos));
				TabLayoutActivityIntent.putExtra("curr_user", loginResponse);
				SelectDatabaseActivity.this
						.startActivity(TabLayoutActivityIntent);
			}

		});

	}

}
